/*
 * 注意：本程序中的“随机”都是伪随机概念，以当前的天为种子。
 */
function random(dayseed, indexseed) {
    var n = dayseed % 11117;
    for (var i = 0; i < 100 + indexseed; i++) {
        n = n * n;
        n = n % 11117;   // 11117 是个质数
    }
    return n;
}

var today = new Date();
var iday = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();

var weeks = ["日","一","二","三","四","五","六"];
var directions = ["北方","东北方","东方","东南方","南方","西南方","西方","西北方"];
var activities = [
    {name:"写单元测试", good:"写单元测试将减少出错",bad:"写单元测试会降低你的开发效率"},
    {name:"洗澡", good:"你几天没洗澡了？",bad:"会把设计方面的灵感洗掉"},
    {name:"临时申请上线", good:"一个报错都没有",bad:"屡推屡挂"},
    {name:"浏览成人网站", good:"重拾对生活的信心",bad:"你会心神不宁"},
    {name:"跟张岭PK代码逻辑", good:"很可能有个结论",bad:"小心被封号"},
    {name:"命名变量\"%v\"", good:"",bad:""},
    {name:"写超过%l行的方法", good:"你的代码组织的很好，长一点没关系",bad:"你的代码将混乱不堪，你自己都看不懂"},
    {name:"提交代码", good:"遇到冲突的几率是最低的",bad:"你遇到的一大堆冲突会让你觉得自己是不是时间穿越了"},
    {name:"推锅给反垃圾", good:"有什么BUG不是反垃圾引起的吗",bad:"想推锅，没戏"},
    {name:"站起来办公", good:"中国人民从此站起来了",bad:"哎，退疼，谁站谁知道"},
    {name:"用Splunk查Log", good:"",bad:"你的Log就存三天，查个卵"},
    {name:"凝视远方", good:"不要想我哦",bad:""},
    {name:"代码复审", good:"发现重要问题的几率大大增加",bad:"你什么问题都发现不了，白白浪费时间"},
    {name:"上Info看文章", good:"能学到不少东西",bad:"好像看看就能懂似的..."},
    {name:"开会", good:"写代码之余放松一下打个盹，有益健康",bad:"小心被扣屎盆子背黑锅"},
    {name:"打DOTA", good:"你将有如神助",bad:"你会被虐的很惨"},
    {name:"晚上上线", good:"晚上是程序员精神最好的时候",bad:"你白天已经筋疲力尽了"},
    {name:"修复BUG", good:"你今天对BUG的嗅觉大大提高",bad:"新产生的BUG将比修复的更多"},
    {name:"中午吃马华", good:"人很少",bad:"你会等饭等到天荒地老"},
    {name:"填报销单", good:"又双叒叕发工资的感觉",bad:"你只报销25块钱吗？"},
    {name:"修改表结构", good:"夜观星象掐指一算，今天可以改",bad:"这个ALTER TABLE语句能跑一年你信不信？"},
    {name:"锻炼一下身体", good:"",bad:"能量没消耗多少，吃得却更多"},
    {name:"在楼道抽烟", good:"抽烟有利于提神，增加思维敏捷",bad:"肯定会被物业抓"},
    {name:"白天上线", good:"今天白天上线是安全的",bad:"可能导致灾难性后果"},
    {name:"重构", good:"代码质量得到提高",bad:"你很有可能会陷入泥潭"},
    {name:"发呆", good:"愣神有助于大脑细胞休息",bad:"我是谁，我在哪，我要干什么"},
    {name:"吟诗", good:"生活不止眼前的苟且，还有诗和远方",bad:"生活不止眼前的苟且，还有5LuK5ZCO55qE6Iuf5LiUCg=="},
    {name:"使用%t", good:"你看起来更有品位",bad:"别人会觉得你在装逼"},
    {name:"听劲爆音乐写代码", good:"开发效率直线上升，整个世界都是你的",bad:"你会错过美女跟你打招呼"},
    {name:"招人", good:"你面前这位有成为牛人的潜质",bad:"这人会写程序吗？"},
    {name:"用张岭写的类库", good:"很大几率可以用",bad:"真的，听哥一句劝，自己写吧"},
    {name:"申请加薪", good:"老板今天心情很好",bad:""},
    {name:"晚上加班", good:"晚上是程序员精神最好的时候",bad:""},
    {name:"在妹子面前吹牛", good:"改善你矮穷挫的形象",bad:"会被识破"},
    {name:"撸Kitty", good:"喵喵喵~",bad:"老子挠死你啊"},
    {name:"撸管", good:"避免缓冲区溢出",bad:"强撸灰飞烟灭"},
    {name:"找肖贺要机器", good:"正好刚有一批",bad:"要机器没有，要命一条"},
    {name:"画流程图", good:"",bad:"完全没思路"},
    {name:"中午吃鹤一", good:"不用等位置",bad:"吃了一个月你还不腻"},
    {name:"设计评审", good:"设计评审会议将变成头脑风暴",bad:"人人筋疲力尽，评审就这么过了"},
    {name:"需求评审", good:"",bad:""},
    {name:"上微博", good:"今天发生的事不能错过",bad:"今天的微博充满负能量"},
    {name:"改张岭写的代码", good:"有几率不会出错",bad:"天啊，你怎么想的，想背锅了吗"},
    {name:"上AB站", good:"还需要理由吗？",bad:"满屏兄贵亮瞎你的眼"},
    {name:"吃鸡", good:"大吉大利，今晚吃鸡",bad:"落地成盒与千里送快递的故事"},
    {name:"吃火锅", good:"有什么事是一顿火锅解决不了的吗？",bad:"你一个星期身上都有火锅味"}
];

var specials = [
    {date:20180101, type:'good', name:'过节', description:'元旦啦~~'},
    {date:20180607, type:'bad', name:'开车狂按喇叭', description:'消停点，你未来的老婆正在高考呢'},
    {date:20180608, type:'bad', name:'开车狂按喇叭', description:'消停点，你未来的丈母娘正在高考呢'},
    {date:20180618, type:'good', name:'买东西', description:'京东618，是时候剁手了'},
    {date:20181111, type:'good', name:'买东西', description:'天猫双11，是时候剁手了'}
];

var tools = ["Vim写程序", "Eclipse写程序", "MSOffice写文档", "记事本写程序", "WindowsXP", "Linux", "MacOS", "IE", "Android设备", "iOS设备"];

var varNames = ["jieguo", "huodong", "pay", "expire", "zhangdan", "every", "free", "i1", "a", "virtual", "ad", "spider", "mima", "pass", "ui", "xxx", "fxck"];

var drinks = ["水","茶","红茶","绿茶","咖啡","奶茶","可乐","鲜奶","豆奶","果汁","果味汽水","苏打水","运动饮料","酸奶","酒","娃哈哈"];

function is_someday() {
    return today.getMonth() == 5 && today.getDate() == 4;
}

function getTodayString() {
    return "( " + today.getFullYear() + "年" + (today.getMonth() + 1) + "月" + today.getDate() + "日 星期" + weeks[today.getDay()] + " )";
}

function star(num) {
    var result = "";
    var i = 0;
    while (i < num) {
        result += "★";
        i++;
    }
    while(i < 5) {
        result += "☆";
        i++;
    }
    return result;
} 

// 生成今日运势
function pickTodaysLuck() {
  var _activities = filter(activities);
    
    var numGood = random(iday, 98) % 3 + 2;
    var numBad = random(iday, 87) % 3 + 2;
    var eventArr = pickRandomActivity(_activities, numGood + numBad);
    
    var specialSize = pickSpecials();
    
    for (var i = 0; i < numGood; i++) {
        addToGood(eventArr[i]);
    }
    
    for (var i = 0; i < numBad; i++) {
        addToBad(eventArr[numGood + i]);
    }
}

// 去掉一些不合今日的事件
function filter(activities) {
    var result = [];
    
    // 周末的话，只留下 weekend = true 的事件
    if (isWeekend()) {
        
        for (var i = 0; i < activities.length; i++) {
            if (activities[i].weekend) {
                result.push(activities[i]);
            }
        }
        
        return result;
    }
    
    return activities;
}

function isWeekend() {
    return today.getDay() == 0 || today.getDay() == 6;
}

// 添加预定义事件
function pickSpecials() {
    var specialSize = [0,0];
    
    for (var i = 0; i < specials.length; i++) {
        var special = specials[i];
        
        if (iday == special.date) {
            if (special.type == 'good') {
                specialSize[0]++;
                addToGood({name: special.name, good: special.description});
            } else {
                specialSize[1]++;
                addToBad({name: special.name, bad: special.description});
            }
        }
    }
    
    return specialSize;
}

// 从 activities 中随机挑选 size 个
function pickRandomActivity(activities, size) {
    var picked_events = pickRandom(activities, size);
    
    for (var i = 0; i < picked_events.length; i++) {
        picked_events[i] = parse(picked_events[i]);
    }
    
    return picked_events;
}

// 从数组中随机挑选 size 个
function pickRandom(array, size) {
    var result = [];
    
    for (var i = 0; i < array.length; i++) {
        result.push(array[i]);
    }
    
    for (var j = 0; j < array.length - size; j++) {
        var index = random(iday, j) % result.length;
        result.splice(index, 1);
    }
    
    return result;
}

// 解析占位符并替换成随机内容
function parse(event) {
    var result = {name: event.name, good: event.good, bad: event.bad};  // clone
    
    if (result.name.indexOf('%v') != -1) {
        result.name = result.name.replace('%v', varNames[random(iday, 12) % varNames.length]);
    }
    
    if (result.name.indexOf('%t') != -1) {
        result.name = result.name.replace('%t', tools[random(iday, 11) % tools.length]);
    }
    
    if (result.name.indexOf('%l') != -1) {
        result.name = result.name.replace('%l', (random(iday, 12) % 247 + 30).toString());
    }
    
    return result;
}

// 添加到“宜”
function addToGood(event) {
    $('.good .content ul').append('<li><div class="name">' + event.name + '</div><div class="description">' + event.good + '</div></li>');
}

// 添加到“不宜”
function addToBad(event) {
    $('.bad .content ul').append('<li><div class="name">' + event.name + '</div><div class="description">' + event.bad + '</div></li>');
}

$(function(){
    if (is_someday()) {document.body.className = 'someday'};
    $('.date').html(getTodayString());
    $('.direction_value').html(directions[random(iday, 2) % directions.length]);
    $('.drink_value').html(pickRandom(drinks,2).join('，'));
    $('.goddes_value').html(star(random(iday, 6) % 5 + 1));
    pickTodaysLuck();
});